package com.college.heyuhua.controller;

import com.college.heyuhua.model.AdminInfo;
import com.college.heyuhua.model.FoundInfo;
import com.college.heyuhua.model.LostInfo;
import com.college.heyuhua.model.Newstyle;
import com.college.heyuhua.model.shop.Category;
import com.college.heyuhua.model.shop.Item;
import com.college.heyuhua.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 赵东坡 on 2019/2/7.
 * Time:10:25
 * 宇宙无敌赵牛逼，祈求永无bug
 */
@Controller
@RequestMapping("/admin")
@SessionAttributes(value = {"name"})
public class AdminController {

    @Autowired
    private IAdminService iAdminService;

    @Autowired
    private ILostService iLostService;

    @Autowired
    private IFoundService iFoundService;

    @Autowired
    private IShopService iShopService;

    @Autowired
    private INewService iNewService;

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/getloginmes")
    public String getloginmes(@RequestParam("aname") String aname,
                              @RequestParam("apassword") String apassword,
                              ModelMap map) {

        System.out.println(aname+apassword);

        AdminInfo a = iAdminService.getloginmes(aname,apassword);
        map.put("loginmessage",a);
        map.put("name",a.getAname());
        return "indexindex";
    }


    @RequestMapping("/introduce")
    public String introduce() {
        return "introduce";
    }

    /*获取丢失物品*/
    @RequestMapping("/deleteLost")
    public String deleteLost(ModelMap map) {
        List<LostInfo> lostInfos =  iLostService.lostList();
        map.put("lostInfos",lostInfos);
        return "deleteLost";
    }

    @RequestMapping("/delete/lid/{lid}")
    public String deleteLost(@PathVariable("lid") String lid, ModelMap map) {
        boolean flag =  iLostService.delete(lid);
        map.put("message","删除成功");
        return "redirect:/admin/deleteLost";
    }


    @RequestMapping("/deleteFound")
    public String deleteFound(ModelMap map) {
        List<FoundInfo> foundInfoList = iFoundService.foundList();
        map.put("foundInfoList",foundInfoList);
        return "deleteFound";
    }


    @RequestMapping("/deletef/fid/{fid}")
    public String deletefound(@PathVariable("fid") String fid, ModelMap map) {
        boolean flag =  iFoundService.deletef(fid);
        map.put("message","删除成功");
        return "redirect:/admin/deleteFound";
    }


    @RequestMapping("/optiongoods")
    public String optiongoods(ModelMap map) {
        List<Category> categoryList = iShopService.getCategoryList();
        map.put("categoryList",categoryList);
        return "optiongoods";
    }


    @RequestMapping("/addgoods/cid/{cid}")
    public String optiongoods(@PathVariable("cid") String cid,ModelMap map) {
        map.put("cid",cid);
        return "addgoods";
    }

    @RequestMapping("/add")
    public String add(@RequestParam("itemname") String itemname,
                      @RequestParam("price") String price,
                      @RequestParam("counts") String counts,
                      @RequestParam("brand") String brand,
                      @RequestParam("PD") String PD,
                      @RequestParam("cid") String cid,ModelMap map) {

        long uid = 1l;
        Item item = new Item();
        item.setItemname(itemname);
        item.setUid(uid);
        item.setBrand(brand);
        item.setCounts(counts);
        item.setPD(PD);
        item.setCid(Long.parseLong(cid));
        item.setPrice(price);

        boolean falg = iShopService.addItem(item);

        return "redirect:/admin/optiongoods";
    }


    @RequestMapping("/addCategory")
    public String addCategory(ModelMap map) {
        List<Category> categoryList = iShopService.getCategoryList();
        map.put("categoryList",categoryList);
        return "addCategory";
    }


    @RequestMapping("/addc")
    public String addc(@RequestParam("cname") String cname) {
        if(StringUtils.isEmpty(cname)) {
            return "redirect:/admin/addCategory";
        }
        boolean flag = iShopService.addCategory(cname);
        return "redirect:/admin/addCategory";
    }


    @RequestMapping("/deletecategory/cid/{cid}")
    public String deletecategory(@PathVariable("cid") String cid) {

        boolean flag = iShopService.deletecategory(cid);

       return  "redirect:/admin/addCategory";
    }


    @RequestMapping("/addstylenews")
    public String addstylenews(ModelMap map) {
        List<Newstyle> newstyleList = iNewService.getstylenews();
        map.put("newstyleList",newstyleList);
        return "addstylenews";
    }

    @RequestMapping("/deletestyle/nsid/{nsid}")
    public String deletestyle(@PathVariable("nsid") String nsid) {
        System.out.println("后端删除类型");
        boolean flag = iNewService.deletestyle(nsid);
        return "redirect:/admin/addstylenews";
    }


    @RequestMapping("/addnews")
    public String addnews(ModelMap map) {
        List<Newstyle> newstyleList = iNewService.getstylenews();
        map.put("newstyleList",newstyleList);
        return "addnews";
    }

    @RequestMapping("/addnewsinfo/nsid/{nsid}")
    public String addnewsinfo(@PathVariable("nsid") String nsid,ModelMap map) {
        System.out.println("添加新闻");
        map.put("nsid",nsid);
        return "addnewsinfo";
    }

    @RequestMapping("/addnewsnifo1")
    public String addnewsnifo(@RequestParam("ntitle")String ntitle,
                              @RequestParam("ncontent") String ncontent,
                              @RequestParam("newsstyleid") String newsstyleid) {
        System.out.println("活动教案");
        boolean flag = iNewService.addnewsinfo(ntitle,ncontent,newsstyleid);
        return "redirect:/admin/addnews";
    }


    @RequestMapping("/addstyle")
    public String addstyle(@RequestParam("stylename") String stylename) {
        boolean falg = iNewService.addstyle(stylename);
        return "redirect:/admin/addstylenews";
    }





}
